package com.atguigu.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Properties;

/**
 * Title: SpringConfig
 * Description: TODO
 *
 * @author gejx
 * @version V1.0
 * @date 2021-12-08
 */
@Configuration
@ComponentScan(basePackages = "com.atguigu.shiwu")
@EnableTransactionManagement//开启事务注解
public class SpringConfig {

    @Bean
    public DruidDataSource getDataSource(){
        DruidDataSource dataSource=  new DruidDataSource();
      dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
      dataSource.setUsername("root");
      dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8");
      dataSource.setPassword("123456");
      return dataSource;
    }

    @Bean
    public JdbcTemplate getjdbcTemplate(DruidDataSource dataSource){
        JdbcTemplate jdbcTemplate=new JdbcTemplate();
      jdbcTemplate.setDataSource(dataSource);
      return jdbcTemplate;
    }
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(DruidDataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager= new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return  dataSourceTransactionManager;
    }
}
